<template>
  <div class="app-container">
    <el-tabs v-model="activeName" @tab-click="handleClick">
      <el-tab-pane v-for="(item,index) in orgTypeOptions" :key="index" :label="item.dictLabel" :name="item.dictValue">
      </el-tab-pane>
    </el-tabs>
    <uc-table url="admin/enterpriseDominant/getInEnterpriseDominantPage" name="主体信息" ref="uctab" pageable table-border
              :search-form="searchForm">
      <template slot="searchForm">
        <el-form size="min" :inline="true" :model="searchForm" class="demo-form-inline filter-container">
          <!--搜索输入框-->
          <el-form-item>
            <el-input type="text" v-model="searchForm.name" placeholder="请输入主体名称" clearable
                      @clear="clearInput()"></el-input>
          </el-form-item>
          <!--搜索按钮--新增按钮-->
          <el-form-item>
            <el-button type="primary" icon="el-icon-search" class="filter-item" @click="handleSearch(all)">查询
            </el-button>
            <el-button type="success" icon="el-icon-plus" class="filter-item"
                       @click="handleAdd()">新 增
            </el-button>
          </el-form-item>
        </el-form>
      </template>
      <template slot="columns">
        <el-table-column prop="name" label="主体名称" align="left" show-overflow-tooltip></el-table-column>
        <el-table-column prop="dominantType" label="主体类型" align="left" :formatter="typeFormat">
        </el-table-column>
        <el-table-column prop="contacts" label="联系人" align="left"></el-table-column>
        <el-table-column prop="mobile" label="联系人电话" align="left"></el-table-column>
        <el-table-column prop="tel" label="主体固话" align="left"></el-table-column>
        <el-table-column prop="maintenanceFlag" label="主体模式" align="left">
          <template slot-scope="scope">
            <el-tooltip class="item" effect="dark" content="点击切换主体状态" placement="top">
              <el-tag class="pointer" @click="handleChangStatus(scope.row)"
                      :type="scope.row.maintenanceFlag?'warning':'success'">
                {{ scope.row.maintenanceFlag ? "维保模式" : "正常监控" }}
              </el-tag>
            </el-tooltip>
          </template>
        </el-table-column>
        <el-table-column prop="address" label="操作" width="240" align="left"
                         class-name="small-padding fixed-width">
          <template slot-scope="scope">
            <!--编辑按钮-->
            <el-button size="mini" icon="el-icon-edit" type="primary" @click="handleEdit(scope.row)">编辑</el-button>
            <el-button size="mini" icon="el-icon-info" type="success" @click="handleInfo(scope.row)">详情</el-button>
            <el-button size="mini" type="danger" icon="el-icon-delete" :loading="deleteLoading"
                       @click="handleDelete(scope.row)">删除
            </el-button>
          </template>
        </el-table-column>
      </template>
    </uc-table>
  </div>
</template>

<script>
import UcTable from "@/components/UcTable/index.vue";
import {deleteInEnterpriseDominant, updateInEnterpriseDominant} from "@/api/enterpriseDominant";

export default {
  name: "enterpriseList",
  components: {UcTable},
  data() {
    return {
      activeName:"6",
      searchForm: {
        deleted: false,
        enterpriseType: null,
        dominantType: 6
      },
      deleteLoading: false,
      ifire: "ifire",
      enter: "enter",
      orgTypeOptions:[],
      mainTypeOptions:[],
    }
  },
  created() {
    if(this.$route.params.type!=null){
      this.searchForm.dominantType=this.$route.params.type
      this.activeName=this.$route.params.type+""
    }
    this.getDicts("org_type").then(res => {
      if (res.data.data) {
        this.orgTypeOptions = res.data.data
      }
    });
    this.getDicts("main_type").then(res=>{
      this.mainTypeOptions = res.data.data
    })
  },
  methods: {
    typeFormat(row){
      return this.selectDictLabel(this.mainTypeOptions,row.dominantType)
    },
    //标签页切换
    handleClick(val) {
      if(val.name === "-1"){
        this.searchForm.dominantType = null
        this.searchForm.customize = "and dominant_type < 5"
      }else{
        this.searchForm.customize = null
        this.searchForm.dominantType = parseInt(val.name)
      }
      this.handleSearch()
    },
    handleSearch() {
      this.$refs.uctab.getTableData(1)
    },
    handleAdd() {
      this.$router.push("/enterprise/enterpriseedit/0?type=" + this.activeName)
    },
    handleEdit(val) {
      this.$router.push("/enterprise/enterpriseedit/" + val.ucId + "?type=" + this.activeName)
    },
    handleInfo(val) {
      this.$router.push("/enterprise/enterpriseinfo/" + val.ucId)
    },
    clearInput() {
      //清除输入框
      this.searchForm.name = null
      this.handleSearch(this.tabValue)
    },
    handleDelete(val) {
      this.$confirm('您确定要执行删除' + val.name + '这个主体吗？', '提醒信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        this.deleteLoading = true
        deleteInEnterpriseDominant(val).then(res => {
          this.deleteLoading = false
          this.$notify.success({
            title: '成功',
            message: "删除主体成功"
          })
          this.handleSearch(this.tabValue)
        }).catch(res => {
          this.deleteLoading = false
          this.$notify.error({
            title: '失败',
            message: res.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消删除'
        });
      });
    },
    handleChangStatus(val) {
      this.$confirm('您确定要执行变更' + val.name + '这个主体状态吗？', '提醒信息', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(() => {
        let obj = Object.assign({}, val)
        obj.maintenanceFlag = !obj.maintenanceFlag
        updateInEnterpriseDominant(obj).then(res => {
          this.$notify.success({
            title: '成功',
            message: "变更主体状态成功"
          })
          this.handleSearch(this.tabValue)
        }).catch(res => {
          this.$notify.error({
            title: '失败',
            message: res.msg
          })
        })
      }).catch(() => {
        this.$message({
          type: 'info',
          message: '已取消变更主体状态'
        });
      });
    },
  }
}
</script>

<style scoped>

</style>
